package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alimama.mobile.sdk.MmuSDK;
import com.alimama.mobile.sdk.config.HandleProperties;
import com.alimama.mobile.sdk.config.MmuSDKFactory;
import com.xm9m.ibczy.mytest.R;

/**
 * @author bingyu
 * @date 2015-03-24
 */
public class JSdkActivity extends Activity {

    private final static String TAG = "JSdkActivity";

    private MmuSDK mmuSDK;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.inset);

		ViewGroup nat = (ViewGroup) findViewById(R.id.nat);
		String slotId = "63845";
		setupAlimama(nat, slotId);

	}

	private void setupAlimama(ViewGroup nat, String slotId) {
		mmuSDK = MmuSDKFactory.getMmuSDK();
		mmuSDK.init(getApplication());
		mmuSDK.accountServiceInit(this);

		ViewGroup handleContainer = (ViewGroup) getLayoutInflater().inflate(
				R.layout.handle_layout, nat, false);
		HandleProperties properties = new HandleProperties(slotId, nat);
		// .setHandleContainer(handleContainer);
		mmuSDK.attach(properties);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		mmuSDK.alimamaJsSdkOnActivityResult(requestCode, resultCode, data);
		if (!mmuSDK.accountServiceHandleResult(requestCode, resultCode, data,this)) {
			super.onActivityResult(requestCode, resultCode, data);
		}
	}

    @Override
    protected void onDestroy() {
        Log.i(TAG, "JSdkActivity::onDestroy");
        mmuSDK.alimamaJsSdkOnDestroy();
        super.onDestroy();
    }

    @Override
	protected void onPause() {
    	Log.i(TAG, "JSdkActivity::onPause");
    	mmuSDK.alimamaJsSdkOnPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		Log.i(TAG, "JSdkActivity::onResume");
		mmuSDK.alimamaJsSdkOnResume();
		super.onResume();
	}

	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.i(TAG, "JSdkActivity::onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.i(TAG, "JSdkActivity:landscape");
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.i(TAG, "JSdkActivity:portrait");
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }

}
